package com.qfedu.service;

import com.qfedu.dao.CollectDao;
import com.qfedu.entity.Collect;
import com.qfedu.entity.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CollectServiceImpl implements CollectService{

    @Autowired
    private CollectDao collectDao;

    /**
     * 返回的指定用户ID的收藏列表
     * @param collect
     * @return
     */
    @Override
    public List<Collect> getCollection(Collect collect) {

        List<Collect> songIdList = collectDao.getCollection(collect);

        if (songIdList != null && songIdList.size() > 0){

            return songIdList;
        }
        return null;
    }

    /**
     * 添加收藏的歌曲 type: 0 代表歌曲， 1 代表歌单
     * @param collect
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ,timeout = 10,readOnly = false)
    @Override
    public ResponseData<String> setCollection(Collect collect) {

        ResponseData<String> responseData = new ResponseData<>();

        //看该用户是否已经收藏过了
        List<Collect> collectionList = collectDao.getCollection(collect);

        if (collectionList.size() == 0){

            collect.setCreateTime(new Date());

            int result = collectDao.setCollection(collect);

            if (result == 1){

                responseData.setCode(1);

            } else {

                responseData.setCode(0);
            }

        } else {

            responseData.setCode(2);
        }

        return responseData;
    }

    @Override
    public ResponseData<String> deleteCollection(Collect collect) {
        ResponseData<String> responseData = new ResponseData<>();
        boolean result = collectDao.deleteCollection(collect);
        responseData.setCode(0);
        if (result) {
            responseData.setCode(1);
        }

        return responseData;
    }
}
